package com.sharedlease.controller;

//import com.sharedlease.utils.CodeUtil;

import cn.hutool.core.date.DateUtil;
import com.mysql.cj.xdevapi.PreparableStatement;
import com.sharedlease.entity.Indent;
import com.sharedlease.entity.Message;
import com.sharedlease.entity.R;
import com.sharedlease.service.UserService;
import com.sharedlease.utils.EmailUtils;
import com.sharedlease.utils.GiteeBucketUtil;
import com.sharedlease.utils.Md5Util;

import com.sharedlease.websocket.ScheduleUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.UUID;

import static com.sharedlease.websocket.MessageConstant.REFUND_MESSAGE;
import static com.sharedlease.websocket.MessageConstant.RENT_REMINDER;


@RestController
@Slf4j
@Api(tags = "第三方工具相关接口")
@RequestMapping("/tools")
public class ToolsController {

    @Autowired
    public JdbcTemplate jdbcTemplate;

    @Autowired
    private GiteeBucketUtil giteeBucketUtil;

    @GetMapping("/sendVerification/{email}")
    @ApiOperation("向邮箱发送验证码并将验证码返回")
    public String sendVerification(@PathVariable String email) {
        String code = "hhh";
        try {
            code = (int) ((Math.random() * 10000) + 500) + "";
            EmailUtils.sendMailForVer(email, code);
        } catch (Exception e) {
            System.out.println("注册发送验证码异常");
        }
        return code;
    }

    @PostMapping("/updateFilePathByIdAndField/{productId}/{pic_pathN}")
    @ApiOperation("（前端上传自定义图片可调用）根据产品id和图片路径字段更改图片")
    @ResponseBody
    public R fileUpload(@RequestParam MultipartFile file, @PathVariable Long productId, @PathVariable String pic_pathN) {
        try {
            String storage_path = giteeBucketUtil.uploadImg(file);
            if (!storage_path.equals("uploadError")) {
                storage_path = "'" + storage_path + "'";
                String sql = "UPDATE product set " + pic_pathN + "=" + storage_path + " where id=" + productId;
                jdbcTemplate.execute(sql);
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("图片上传失败，{}", file.getOriginalFilename());
        }
        return R.error();
    }


    @PostMapping("/sendPasswordToEmail/{email}/{encodedPwd}")
    @ApiOperation("向邮箱发送明文密码")
    public void forgetPassword(@PathVariable String email, @PathVariable String encodedPwd) {
//        if ("-1".equals(email)){
//            // 返回错误信息
//        }
        // 通过邮箱查密码 ,

        String password = new String(Base64Utils.decodeFromUrlSafeString(encodedPwd));
        try {
            EmailUtils.sendEmailForPsd(email, password);
        } catch (Exception e) {
            System.out.println("邮件发送失败");
            // 返回错误信息
        }
        // 返回正确提示
    }

//    @PostMapping("/sendRefundMessage/{destId}")
//    @ApiOperation("向目标id发送一条订单记录")
//    public R sendRefundMessage(@PathVariable String destId,@RequestBody Indent indent){
//        try {
//
//            //String userId, String username, Integer rentTimeEnd, String message, String time
//            System.out.println(indent.toString());
//            ScheduleUtil.sendRefundMessage(new Message(destId,indent));
//            return R.ok();
//        } catch (SchedulerException | InterruptedException e) {
//            e.printStackTrace();
//           return R.error();
//        }
//    }


}
